<!DOCTYPE html>

<html lang="zh-CN">
    <head>
        <meta charset="UTF-8" />
        <meta
            name="viewport"
            content="width=device-width, initial-scale=1.0, user-scalable=no"
        />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>JavaScript数组</title>
    </head>

    <body>
        <h1>JavaScript数组</h1>
        <script>
            // let arr1 = new Array(1, 3, 4, 4, 5, 3, 5, 32, 6, 67, 4, 9);
            // console.log(arr1); //  [1, 3, 4, 4, 5, 3, 5, 32, 6, 67, 4, 9]

            // let arr2 = [1, 3, 4, 4, 5, 3, 5];
            // console.log(arr2); // [1, 3, 4, 4, 5, 3, 5];

            // console.log(arr1.length); // 12

            // console.log(arr1.push(90, 99));
            // console.log(arr1);

            // console.log(arr1.pop());
            // console.log(arr1);

            // console.log(arr1.unshift(55, 66));
            // console.log(arr1);

            // console.log(arr1.shift());
            // console.log(arr1);

            // // 删除元素，返回被删除的元素
            // console.log(arr1.splice(1, 3));
            // console.log(arr1);

            // // 替换元素，返回被替换的元素
            // console.log(arr1.splice(1, 3, 1, 2, 3, 4, 5)); // [4, 5, 3]
            // console.log(arr1); // [66, 1, 2, 3, 4, 5, 5, 32, 6, 67, 4, 9, 90]

            // // 插入元素，返回空数组
            // console.log(arr1.splice(1, 0, 22, 33, 44)); // []
            // console.log(arr1); // [66, 22, 33, 44, 1, 2, 3, 4, 5, 5, 32, 6, 67, 4, 9, 90]

            // console.log(arr1); // [66, 22, 33, 44, 1, 2, 3, 4, 5, 5, 32, 6, 67, 4, 9, 90]
            // console.log(arr1.reverse()); // [90, 9, 4, 67, 6, 32, 5, 5, 4, 3, 2, 1, 44, 33, 22, 66]

            // // 降序排序
            // console.log(arr1.sort((a, b) => b - a)); // [90, 67, 66, 44, 33, 32, 22, 9, 6, 5, 5, 4, 4, 3, 2, 1]

            // // 升序排序
            // console.log(arr1.sort((a, b) => a - b)); // [1, 2, 3, 4, 4, 5, 5, 6, 9, 22, 32, 33, 44, 66, 67, 90]

            // console.log(arr1.fill(444, 2, 4)); // [1, 2, 444, 444, 4, 5, 5, 6, 9, 22, 32, 33, 44, 66, 67, 90]

            // // console.log(arr1); // [1, 2, 444, 444, 4, 5, 5, 6, 9, 22, 32, 33, 44, 66, 67, 90]
            // // console.log(arr1.copyWithin(-5)); // [1, 2, 444, 444, 4, 5, 5, 6, 9, 22, 32, 1, 2, 444, 444, 4]
            // // console.log(arr1); // [1, 2, 444, 444, 4, 5, 5, 6, 9, 22, 32, 33, 44, 66, 67, 90]
            // // console.log(arr1.copyWithin(0, 5)); // [5, 5, 6, 9, 22, 32, 33, 44, 66, 67, 90, 33, 44, 66, 67, 90]
            // console.log(arr1); // [1, 2, 444, 444, 4, 5, 5, 6, 9, 22, 32, 33, 44, 66, 67, 90]
            // console.log(arr1.copyWithin(-2, -8, -4)); // [1, 2, 444, 444, 4, 5, 5, 6, 9, 22, 32, 33, 44, 66, 9, 22]

            // console.log("------------------------------");

            // console.log(arr1); // [1, 2, 444, 444, 4, 5, 5, 6, 9, 22, 32, 33, 44, 66, 67, 90]
            // console.log(arr1.slice(3, 10)); // [444, 4, 5, 5, 6, 9, 22]
            // console.log(arr1.slice(-10, -3)); //[5, 6, 9, 22, 32, 33, 44]

            // console.log(arr1.join()); // 1,2,444,444,4,5,5,6,9,22,32,33,44,66,9,22
            // console.log(arr1.join("-")); // 1-2-444-444-4-5-5-6-9-22-32-33-44-66-9-22

            // console.log(arr1.toString()); // 1,2,444,444,4,5,5,6,9,22,32,33,44,66,9,22

            // console.log(arr1.indexOf(444));

            // console.log(arr1.lastIndexOf(444, -5));

            // // console.log(arr1.concat(123, 335));
            // // [1, 2, 444, 444, 4, 5, 5, 6, 9, 22, 32, 33, 44, 66, 9, 22, 123, 335]

            // console.log(arr1.concat([45, 33]));
            // // [1, 2, 444, 444, 4, 5, 5, 6, 9, 22, 32, 33, 44, 66, 9, 22, 45, 33]

            // console.log(arr1.includes(3, -5));

            // let arr3 = [10, 23, 33];

            // console.log(
            //     arr3.toLocaleString("zh", {
            //         style: "currency",
            //         currency: "CNY",
            //     })
            // );
            // console.log(
            //     arr3.toLocaleString("zh", {
            //         style: "percent",
            //     })
            // );

            // arr1.forEach((item, index) => {
            //     console.log(index, item);
            // });

            // console.log(arr1); // [1, 2, 444, 444, 4, 5, 5, 6, 9, 22, 32, 33, 44, 66, 9, 22]
            // let arr4 = arr1.map((item) => {
            //     return item * 2;
            // });
            // console.log(arr4); // [2, 4, 888, 888, 8, 10, 10, 12, 18, 44, 64, 66, 88, 132, 18, 44]

            // let arr5 = arr4.filter((item) => {
            //     return item > 50;
            // });
            // console.log(arr5); // [888, 888, 64, 66, 88, 132]

            // let res6 = arr4.find((item) => {
            //     return item > 50;
            // });
            // console.log(res6); // 888

            // let res7 = arr4.findIndex((item) => item > 50);
            // console.log(res7); // 2

            // arr5.push("abc");

            // let res = arr5.every((item) => {
            //     return typeof item === "number";
            // });
            // console.log(res);

            // let res1 = arr5.some((item) => typeof item === "number");
            // console.log(res1);

            // let arr = [1, 3, 4, 4];

            // let res = arr.reduce((acc, cur, idx, src) => {
            //     console.log("acc:", acc);
            //     console.log("cur:", cur);
            //     console.log("idx:", idx);
            // });
            // console.log(res);

            // 数组求和
            // let arr = [1, 3, 4, 4];

            // let sum = arr.reduce((acc, cur) => acc + cur, 0);
            // console.log(sum); // 12

            // // 统计数组对象中的值
            // let arr = [{ x: 1 }, { x: 5 }, { x: 9 }];
            // let sum = arr.reduce((a, b) => a + b.x, 0);
            // console.log(sum); // 15

            // // 二维数组扁平化
            // let arr = [
            //     [1, 3],
            //     [3, 3],
            //     [4, 3],
            // ];
            // let res = arr.reduce((a, b) => a.concat(b), []);
            // console.log(res); //  [1, 3, 3, 3, 4, 3]

            // 统计元素出现的次数
            // let names = ["Alice", "Bob", "Tiff", "Bruce", "Alice"];

            // let res = names.reduce((a, b) => {
            //     if (b in a) {
            //         a[b]++;
            //     } else {
            //         a[b] = 1;
            //     }
            //     return a;
            // }, {});
            // console.log(res);

            let arr = [
                [1, 3],
                [3, 3],
                [4, 3],
            ];
            let res = arr.flat();
            console.log(res); //  [1, 3, 3, 3, 4, 3]

            let arr1 = [
                [1, 3],
                [3, 3],
                [4, [3, 5]],
            ];

            console.log(arr1.flat(2)); //  [1, 3, 3, 3, 4, 3, 5]
        </script>
    </body>
</html>
